package com.glory.dgcpcpda.activity.mfg;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.technicalservice.TechnicalDemandActivity;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.SchedulingEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class EquipmentSchedulingActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvLocation)
    TextView tvLocation;
    @BindView(R.id.tvDepartment)
    TextView tvDepartment;
    @BindView(R.id.tvEqpID)
    TextView tvEqpID;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.flType)
    FrameLayout flType;
    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.ivTypeArrow)
    ImageView ivTypeArrow;

    private SchedulingEntity.EQUIPMENTLISTDTO mData;
    private FreeUI_GeneralPop mTypePop;
    private ArrayList<String> mTypeList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_equipment_scheduling;
    }

    @Override
    protected void initView() {
        tvTitle.setText("设备调度");
        setMainLayout(llMain);

        Bundle bundle = getIntent().getExtras();
        mData = (SchedulingEntity.EQUIPMENTLISTDTO) bundle.getSerializable("DATA");
        tvLocation.setText(mData.getLocation());
        tvDepartment.setText(mData.getDepartment());
        tvEqpID.setText(mData.getEquipmentid());
        tvEqpType.setText(mData.getEqptype());

        mTypeList.add("转正常生产");
        mTypeList.add("工程借机");
        mTypeList.add("计划停机");
        mTypeList.add("计划停机结束");
        mTypeList.add("更改产品");
        mTypeList.add("更换批次项");
        mTypeList.add("PM");

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit, R.id.flType})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (mData == null){
                    showToast("设备信息为空，请返回重试",false);
                    return;
                }
                if (tvType.getText().toString().equals("")){
                    showToast("请选择类型",false);
                    return;
                }
                switch (tvType.getText().toString()){
                    case "更改产品":
                        Bundle bundle = new Bundle();
                        FreeApi_Utils.skipWithExtraIntent("", bundle, EquipmentSchedulingActivity.this, TechnicalDemandActivity.class);
                        break;
                    case "PM":
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("EQPID", tvEqpID.getText().toString());
                        FreeApi_Utils.skipWithExtraIntent("", bundle1, EquipmentSchedulingActivity.this, PMSchedulingActivity.class);
                        break;
                    default:
                        FreeUI_EntityCallBack<BaseResponseBean<JsonObject>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<JsonObject>>
                                (new TypeToken<BaseResponseBean<JsonObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    showToast("提交成功", false);
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JsonObject>> response) {
                                super.onError(response);
                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE", "CHANGELOGEVENT");
                        map.addProperty("EQUIPMENTID", tvEqpID.getText().toString());
                        map.addProperty("DISPATCHTYPE", tvType.getText().toString());
                        NetUtils.requestPostNet("CPC.CHANGELOGEVENT", "TRANSFERMES", this, map, callback);
                        break;
                }
                break;
            case R.id.flType:
                if (mTypePop != null) {
                    if (mTypePop.isShowing()) {
                        mTypePop.dismiss();
                    }
                } else {
                    mTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flType.getWidth(), mTypeList, ivTypeArrow, (codeS, i) -> {
                        tvType.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivTypeArrow);
                mTypePop.showAsDropDown(flType, 0, 0);
                break;
        }
    }

}
